class ZJHGameEffectPanel extends how.module.View 
{
    public tableCard: eui.Group;
    public handCardPanel: eui.Group;
    private startCardEffectCB: { cb: () => void, thisObj: any };
    public static choumaList: string[] = [
        "10_png",
        "20_png",
        "50_png",
        "100_png",
        "200_png",
        "500_png",
        "1000_png",
        "2000_png",
        "5000_png",
        "10000_png",
        "20000_png",
    ];
    public constructor() {
        super();
        this.touchEnabled = this.touchChildren = false;
    }
    private choumaList: ZJHChouma[] = [];
    /**
     * 跟注/加注筹码特性
     */
    public playChoumaAddEffect(playerView: ZJHPlayerView, score: number, choumaArea: eui.Group): void {
        var choumaconfig: number[] = [10, 20, 50, 100, 200, 500, 1000, 2000, 5000, 10000, 20000];
        var choumaMoneyList: number[] = this.getChipDataList(score, choumaconfig);
        for (var i: number = 0; i < choumaMoneyList.length; i++) {
            var chouma: ZJHChouma = new ZJHChouma();
            chouma.source = choumaMoneyList[i] + "_png";
            chouma.money = choumaMoneyList[i];
            this.addChild(chouma);
            var startPoint: egret.Point = playerView.moneyLabel.localToGlobal(playerView.moneyLabel.width / 2, playerView.moneyLabel.height / 2);
            chouma.x = startPoint.x - chouma.width / 2;
            chouma.y = startPoint.y - chouma.height / 2;
            var endX: number = how.Random.range(choumaArea.x, choumaArea.x + choumaArea.width - chouma.width);
            var endY: number = how.Random.range(choumaArea.y, choumaArea.y + choumaArea.height - chouma.height);
            egret.Tween.get(chouma).to({ x: endX, y: endY }, 300);
            this.choumaList.push(chouma);
        }
        how.SoundManager.playEffect("zjh_money_mp3");
    }
    /**
     * 播放赢牌筹码
     */
    public playChoumaWinEffect(playerView: ZJHPlayerView): void {
        for (var i: number = 0; i < this.choumaList.length; i++) {
            var endPoint: egret.Point = playerView.moneyLabel.localToGlobal(playerView.moneyLabel.width / 2, playerView.moneyLabel.height / 2);
            var chouma: ZJHChouma = this.choumaList[i];
            egret.Tween.removeTweens(chouma);
            var tween: egret.Tween = egret.Tween.get(chouma).
                wait(i * 10).
                to({ x: endPoint.x - chouma.width / 2, y: endPoint.y - chouma.height / 2 }, 300).
                call((currentChouma: ZJHChouma) => {
                    egret.Tween.removeTweens(currentChouma);
                    this.removeChild(currentChouma);
                }, this, [chouma]);
            if (i == this.choumaList.length - 1) {
                tween.call(() => {
                    this.choumaList = [];
                }, this);
            }
        }
        how.SoundManager.playEffect("zjh_getMoney_mp3");
    }
    private getChipDataList(money: number, choumaconfig: number[]): number[] {
        var result: number[] = [];
        var tempBase: number[] = [];
        var betBase: number[] = choumaconfig;
        tempBase.length = betBase.length;
        for (var i = 0; i < betBase.length; i++) {
            tempBase[i] = betBase[i];
        }
        // tempBase.sort(function (a: number, b: number): number//从大到小排序
        // {
        //     return a > b ? 1 : -1;
        // });
        while (money > 0) {
            for (var j = tempBase.length - 1; j >= 0; j--) {
                if (tempBase[j] <= money) {
                    if (tempBase[j] <= 0) {
                        money = 0;
                    }
                    else {
                        money -= tempBase[j];
                        var index = Math.floor(betBase.indexOf(tempBase[j]) / 10);
                        result.push(tempBase[j]);
                        break;
                    }
                }

            }
        }
        return result;
    }
    /**
     * 播放洗牌动画
     */
    public playStartCardEffect(callBack: () => void, thisObj: any): void {
        for (var i: number = 0; i < 30; i++) {
            var card: eui.Image = this.tableCard.getChildAt(i) as eui.Image;
            card.visible = true;
            card.x = card.anchorOffsetX;
            card.y - card.anchorOffsetY;
            var tween: egret.Tween = egret.Tween.get(card).wait(15 * i).to({ x: i * 5 }, 15);
            if (i == 29) {
                tween.call(this.playStartCardEffectComplete, this);
            }
        }
        this.startCardEffectCB = { cb: callBack, thisObj: thisObj };
    }
    /**
    * 洗牌动画完成，准备发牌
    */
    private playStartCardEffectComplete(): void {
        var playerList: ZJHPlayerData[] = (<ZJHPlayerData[]> AppData.getInstance().gameData.getPlayersByTableIDHaveChairID());
        for (var i: number = 0; i < playerList.length; i++) {
            var playerView: ZJHPlayerView = how.Application.currentScene["player" + playerList[i].seatID];
            var playerHandCardPanel: eui.Group = playerView.haveHandCardPanel ? playerView.handCardPanel : this.handCardPanel;
            for (var j: number = 0; j < playerHandCardPanel.numChildren; j++) {
                var index: number = i + j;
                var startCard: eui.Image = this.tableCard.getChildAt(this.tableCard.numChildren - index - 1) as eui.Image;
                startCard.visible = false;
                var startPoint: egret.Point = startCard.localToGlobal(0, 0);
                var endCard: eui.Image;
                endCard = playerHandCardPanel.getChildAt(j) as eui.Image;
                var endPoint: egret.Point = endCard.localToGlobal(0, 0);
                this.addPlayerCardAndPlayEffect(
                    new egret.Rectangle(startPoint.x, startPoint.y, startCard.width, startCard.height),
                    new egret.Rectangle(endPoint.x, endPoint.y, endCard.width, endCard.height),
                    index, endCard, index == playerList.length + playerHandCardPanel.numChildren - 2);
            }
        }
    }
    /**
     * 发单张牌
     */
    private addPlayerCardAndPlayEffect(startSize: egret.Rectangle, endSize: egret.Rectangle, index: number, endCard: eui.Image, isEnd: boolean): void {
        var card: eui.Image = new eui.Image();
        card.source = "card_0_png";
        var startCard: eui.Image = this.tableCard.getChildAt(0) as eui.Image;
        card.x = startSize.x + startSize.width / 2;
        card.y = startSize.y + startSize.height / 2;
        card.anchorOffsetX = startSize.width / 2;
        card.anchorOffsetY = startSize.height / 2;
        card.width = startSize.width;
        card.height = startSize.height;
        this.addChild(card);
        var tween: egret.Tween = egret.Tween.get(card).wait(index * 150).
            call(() => {
                how.SoundManager.playEffect("zjh_sendcard_mp3");
            }, this).
            to({
                x: endSize.x + endSize.width / 2,
                y: endSize.y + endSize.height / 2,
                width: endSize.width,
                height: endSize.height,
                rotation: 360 * 2,
                anchorOffsetX: endSize.width / 2,
                anchorOffsetY: endSize.height / 2
            }, 300);
        tween.call(this.playerOneCardEffectComplete, this, [card, endCard, isEnd]);
    }
    private playerOneCardEffectComplete(effectCard: eui.Image, endCard: eui.Image, isEnd: boolean): void {
        effectCard.parent.removeChild(effectCard);
        endCard.visible = true;
        if (isEnd) {
            for (var i: number = 0; i < this.tableCard.numChildren; i++) {
                this.tableCard.getChildAt(i).visible = false;
            }
            this.startCardEffectCB.cb.call(this.startCardEffectCB.thisObj);
            this.startCardEffectCB = null;
        }
    }
    public choumaEffect(): void {

    }
    private createChouma(): void {
    }
    public playJiazhuChoumaEffect(startPoint: egret.Point, endPoint: egret.Point, minX: number, maxX: number, minMaxY: number): void {
        var choumaCount: number = Math.floor(how.Random.range(10, 25));
        for (var i: number = 0; i < choumaCount; i++) {
            var chouma: eui.Image = new eui.Image();
            var source = ZJHGameEffectPanel.choumaList[Math.floor(Math.random() * ZJHGameEffectPanel.choumaList.length)];
            chouma.source = source;
            this.addChild(chouma);
            chouma.x = startPoint.x - chouma.width / 2;
            chouma.y = startPoint.y - chouma.height / 2;
            var endX: number = Math.floor(how.Random.range(minX, maxX)) - chouma.width / 2;
            var endY: number = endPoint.y - chouma.height / 2 + Math.floor(how.Random.range(-minMaxY, minMaxY));
            egret.Tween.get(chouma).
                to({ x: endX, y: endY }, 300).
                to({ alpha: 0 }, 300).
                call(this.onJiazhuChoumaComplete, this, [chouma]);
        }
    }
    private onJiazhuChoumaComplete(chouma: eui.Image): void {
        chouma.parent.removeChild(chouma);
    }
    /**
     * 播放比牌特效
     */
    public playCompareCard(players: ZJHPlayerView[], lostIndex: number): void {
        var mask: eui.Rect = new eui.Rect();
        mask.fillColor = 0x000000;
        mask.fillAlpha = 0.7;
        mask.strokeWeight = 0;
        mask.percentHeight = mask.percentWidth = 100;
        this.addChild(mask);
        var playerImages: eui.Image[] = [];
        for (var i: number = 0; i < players.length; i++) {
            var player: ZJHPlayerView = players[i];
            var renderTexture: egret.RenderTexture = new egret.RenderTexture();
            renderTexture.drawToTexture(player.headContainer, new egret.Rectangle(0, 0, player.headContainer.width, player.headContainer.height));
            var playerImage: eui.Image = new eui.Image();
            playerImage.source = renderTexture;
            this.addChild(playerImage);
            var globalPoint: egret.Point = player.headContainer.localToGlobal(0, 0);
            var localPoint: egret.Point = playerImage.globalToLocal(globalPoint.x, globalPoint.y);
            playerImage.anchorOffsetX = playerImage.width / 2;
            playerImage.anchorOffsetY = playerImage.height / 2;
            playerImage.x = localPoint.x + playerImage.width / 2;
            playerImage.y = localPoint.y + playerImage.height / 2;
            playerImages.push(playerImage);
        }
        for (var i: number = 0; i < playerImages.length; i++) {
            var tween: egret.Tween = egret.Tween.get(playerImages[i]).
                to({ scaleX: 1.5, scaleY: 1.5 }, 300);
            if (i == playerImages.length - 1) {
                tween.call(() => {
                    how.SoundManager.playEffect("zjh_shangdian_mp3");
                    var compareCardAni: how.Animation = new how.Animation();
                    compareCardAni.loop = false;
                    compareCardAni.frameRate = 60;
                    compareCardAni.frameNum = 13;
                    compareCardAni.animationSource = "zjh_bipai_{0}_png";
                    compareCardAni.horizontalCenter = 0;
                    compareCardAni.verticalCenter = 0;
                    this.addChild(compareCardAni);
                    this.setChildIndex(compareCardAni, this.getChildIndex(mask) + 1);
                    compareCardAni.once(egret.Event.ENDED, () => {
                        var shoujiAni: how.Animation = new how.Animation();
                        shoujiAni.loop = false;
                        shoujiAni.frameRate = 60;
                        shoujiAni.frameNum = 8;
                        shoujiAni.width = 250;
                        shoujiAni.height = 250;
                        shoujiAni.animationSource = "zjh_shouji_{0}_png";
                        this.addChild(shoujiAni);
                        var lostPlayer: eui.Image = playerImages[lostIndex];
                        shoujiAni.x = lostPlayer.x - shoujiAni.width / 2;
                        shoujiAni.y = lostPlayer.y - shoujiAni.height / 2;
                        shoujiAni.once(egret.Event.ENDED, () => {
                            mask.parent.removeChild(mask);
                            while (playerImages.length) {
                                var playerImage: eui.Image = playerImages.shift();
                                playerImage.parent.removeChild(playerImage);
                            }
                            compareCardAni.parent.removeChild(compareCardAni);
                            shoujiAni.parent.removeChild(shoujiAni);
                        }, this);
                    }, this);
                }, this);
            }
            tween.to({ scaleX: 1, scaleY: 1 }, 300);
        }
    }
}